import 'package:flutter/material.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/home_business/widget/collect_status_wrapper.dart';
import 'package:tiktokadult/hj_page/home_business/widget/user_avatar.dart';
import 'package:tiktokadult/hj_page/home_business/widget/user_name_view.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';
import 'package:tiktokadult/hj_utils/date_time_util.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';

import '../../community_business/widget/follow_loading_button.dart';

class UserInfoView extends StatefulWidget {
  final VideoModel? model;
  final bool showFollow;
  final bool isDarkStyle;

  const UserInfoView({
    super.key,
    this.model,
    this.showFollow = true,
    this.isDarkStyle = false,
  });

  @override
  State<StatefulWidget> createState() {
    return _UserInfoViewState();
  }
}

class _UserInfoViewState extends State<UserInfoView> {
  VideoModel? get model => widget.model;

  bool _isFollowLoading = false;

  void _followEvent() async {
    try {
      if (_isFollowLoading) return;
      _isFollowLoading = true;
      final res = await MineService.getFollow(model?.publisher?.uid, !(model?.publisher?.hasFollowed ?? false));
      if (res) {
        model?.publisher?.hasFollowed = !(model?.publisher?.hasFollowed ?? false);
      }
    } catch (e) {
      debugLog(e);
    }
    _isFollowLoading = false;
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    if (model == null || model?.publisher == null) return SizedBox.shrink();
    final isVIP = ((model?.publisher?.isVip ?? false) || (model?.publisher?.vipLevel ?? 0) > 0);
    return SizedBox(
      height: 45,
      child: Row(
        children: [
          UserAvatar(
            model: model?.publisher,
            size: 45,
          ),
          11.sizeBoxW,
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                  children: [
                    Flexible(
                      child: UserNameView(
                        name: model?.publisher?.name,
                        isVip: isVIP,
                        isOfficial: model?.publisher?.officialCert,
                      ),
                    ),
                    if (model?.publisher?.officialCert == true) ...[
                      4.sizeBoxW,
                      Image.asset("official_icon.png".commonImgPath, width: 22, height: 22)
                    ],
                  ],
                ),
                4.sizeBoxH,
                Row(
                  children: [
                    if (isVIP && model?.publisher?.vipName?.isNotEmpty == true) ...[
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                        decoration: BoxDecoration(
                          color: Color(0xffFFD5A8),
                          borderRadius: BorderRadius.circular(30),
                        ),
                        child: Text(
                          "${model?.publisher?.vipName}",
                          style: TextStyle(color: Colors.black, fontSize: 10),
                        ),
                      ),
                      6.sizeBoxW,
                    ],
                    Text(
                      formatTimeTwo(model?.reviewAt),
                      style: TextStyle(color: Color(0xff999999), fontSize: 12),
                    ),
                  ],
                ),
              ],
            ),
          ),
          if (widget.showFollow)
            CollectStatusWrapper(
              videoModel: model,
              builder: () {
                return InkWell(
                  onTap: () => _followEvent(),
                  child: FollowLoadingButton(
                    isFollow: model?.publisher?.hasFollowed ?? false,
                    isLoading: _isFollowLoading,
                    isDarkStyle: widget.isDarkStyle,
                  ),
                );
              },
            ),
        ],
      ),
    );
  }
}
